import pbjs from "protobufjs/cli/pbjs";
import pbts from "protobufjs/cli/pbts";
import { log, ensureDir } from "./utils";
import fs from "fs";
import path from "path";
const args = [
  "-t",
  "static-module",
  "-w",
  "es6",
  "--es6",
  "--no-create",
  "--no-encode",
  "--no-verify",
];
const protoDirPath = path.join(__dirname, "../proto");
const staticDirPath = path.join(__dirname, "../decoder");
const protoFilePaths = fs.readdirSync(protoDirPath);
ensureDir(staticDirPath);
protoFilePaths.forEach(async (pfp, index) => {
  console.log(`----- ${index + 1} of ${protoFilePaths.length} -----`);
  const name = path.parse(pfp).name;
  const temp = args.map((v) => v);
  temp.push(path.join(protoDirPath, `${name}.proto`));
  pbjs.main(temp, (err, output) => {
    if (err) {
      log(name, err);
      return;
    }
    const jsPath = path.join(staticDirPath, `${name}.js`);
    fs.writeFileSync(jsPath, output);
    pbts.main([jsPath], (err, output) => {
      if (err) {
        log(name, err);
        return;
      }
      fs.writeFileSync(path.join(staticDirPath, `${name}.d.ts`), output);
    });
  });
});
